package ch.headshot.photomap.client.gpx.impl;

import java.util.ArrayList;
import java.util.List;

import ch.headshot.photomap.client.gpx.TrackSegment;
import ch.headshot.photomap.client.gpx.WayPoint;

import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NodeList;

public class TrackSegmentImpl implements TrackSegment {

	private static final String TRKPT = "trkpt";
	private final Element item;
	private ArrayList<WayPoint> points;

	public TrackSegmentImpl(Element item) {
		assert item != null;
		this.item = item;
	}

	@Override
	public String toString() {
		StringBuffer b = new StringBuffer("<trkseg>");
		for (WayPoint point : getPoints()) {
			b.append(point);
		}
		b.append("</trkseg>");
		return b.toString();
	}

	public TrackSegmentImpl() {
		item = null;
	}

	public List<WayPoint> getPoints() {
		if (points == null) {
			points = new ArrayList<WayPoint>();
			if (item != null) {
				NodeList rtes = item.getElementsByTagName(TRKPT);
				for (int i = 0; i < rtes.getLength(); i++) {
					points.add(new WayPointImpl((Element) rtes.item(i)));
				}
			}

		}

		return points;
	}

}
